﻿@page "/"

@using System.Dynamic;

@inject EmployeeService EmployeeService

<TelerikGrid Data="@GridData"
             Pageable="true"
             Sortable="true"
             Groupable="true"
             FilterMode="@GridFilterMode.FilterMenu"
             OnUpdate="@UpdateHandler"
             OnCreate="@CreateHandler"
             OnDelete="@DeleteHandler">
    <GridColumns>
        @*Field Type is mandatory when you are binding to ExpandoObject or Dictionary*@
        <GridColumn Field="EmployeeId" FieldType="@typeof(int)" Editable="false"></GridColumn>
        <GridColumn Field="FirstName" FieldType="@typeof(string)"></GridColumn>
        <GridColumn Field="Salary" FieldType="@typeof(decimal)">
            <FooterTemplate>
                Highest salary: @context.Max?.ToString("C0")
            </FooterTemplate>
            <GroupFooterTemplate>
                Highest salary in this group: @context.Max?.ToString("C0")
            </GroupFooterTemplate>
        </GridColumn>
        <GridColumn Field="IsActive" FieldType="@typeof(bool)">
            <GroupHeaderTemplate>
                IsActive: @context.Value - count: @context.Count
            </GroupHeaderTemplate>
        </GridColumn>
        <GridColumn Field="HireDate" FieldType="@typeof(DateTime)"></GridColumn>
        <GridCommandColumn>
            <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
            <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
            <GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
            <GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton>
        </GridCommandColumn>
    </GridColumns>
    <GridAggregates>
        <GridAggregate Field="IsActive" Aggregate="@GridAggregateType.Count" />
        <GridAggregate Field="Salary" Aggregate="@GridAggregateType.Max" />
    </GridAggregates>
    <GridToolBar>
        <GridCommandButton Command="Add" Icon="add">Add Employee</GridCommandButton>
    </GridToolBar>
</TelerikGrid>

@code {
    public List<ExpandoObject> GridData { get; set; }

    protected override async Task OnInitializedAsync()
    {
        GridData = await EmployeeService.GetEmployeesAsync();
    }

    // CUD operations happen as usual - the grid events provide you with the model that the user works on
    // so you can cast it to its corresponding type and perform the actual data source operations
    // https://docs.telerik.com/blazor-ui/components/grid/editing/overview
    // this example shows one way of updating the view-model data
    // you should also update the actual data source (e.g., through a data service)
    public void UpdateHandler(GridCommandEventArgs args)
    {
        var model = (ExpandoObject)args.Item;

        var matchingItem = GridData.Where(itm => (itm as dynamic).EmployeeId == (model as dynamic).EmployeeId).FirstOrDefault();

        if (matchingItem != null)
        {
            // ?? null will handles setting the default value if you leave the Grid column empty
            ((dynamic)matchingItem).FirstName = (model as dynamic).FirstName ?? null;
            //((dynamic)matchingItem).LastName = (model as dynamic).LastName; // we don't define this field in the markup so it is not available in the grid for editing, which is why we don't update it here
            ((dynamic)matchingItem).IsActive = (model as dynamic).IsActive ?? false; ;
            ((dynamic)matchingItem).HireDate = (model as dynamic).HireDate ?? DateTime.Now;
            ((dynamic)matchingItem).Salary = (model as dynamic).Salary ?? 0; ;
        }
    }

    public void DeleteHandler(GridCommandEventArgs args)
    {
        var model = (ExpandoObject)args.Item;

        GridData.Remove(model);
    }

    public void CreateHandler(GridCommandEventArgs args)
    {
        var model = (ExpandoObject)args.Item;
        ((dynamic)model).EmployeeId = GridData.Count() + 1;

        // When the values are not set in the Grid columns, we receive the ExpandoObject without fields.
        // We need to handle setting default values by ourself
        SetExpandoDefaultValue(model, "FirstName", default(string));
        SetExpandoDefaultValue(model, "IsActive", false);
        SetExpandoDefaultValue(model, "HireDate", DateTime.Now);
        SetExpandoDefaultValue(model, "Salary", 0);

        GridData.Insert(0, model);
    }

    public void SetExpandoDefaultValue<T>(ExpandoObject model, string key, T value)
    {
        if (!(model as IDictionary<string, object>).ContainsKey(key))
        {
            (model as IDictionary<string, object>)[key] = value;
        }
    }
}